//
//  main.m
//  Demo0018
//
//  Created by zTercel on 13-1-30.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//
//  ReadMe: 统计数值出现的次数

#import <Foundation/Foundation.h>

@interface NSMutableDictionary (NSPrinting)
-(BOOL) cotainValue: (id) value;
@end

@implementation NSMutableDictionary(NSPrinting)

-(BOOL) cotainValue:(id)value
{
    BOOL bContain = NO;
    if ([self count] != 0)
    {
        for (id key in self)
        {
            if ([key isEqual:value])
            {
                bContain = YES;
                break;
            }
        }
    }
    return bContain;
}
@end

NSDictionary* countOfValue(NSArray* array)
{
    NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithCapacity:100];
    if ([array count] > 0)
    {
        for (NSNumber* numberValue in array)
        {
            unsigned valueCount     = 1;
            if ([dictionary cotainValue:numberValue])
            {
                valueCount = [[dictionary objectForKey:numberValue]intValue];
                valueCount++;
            }
            [dictionary setObject:[NSNumber numberWithInt:valueCount] forKey:numberValue];
        }

    }
    return dictionary;
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        int values[]    = {100, 122, 1234, 122, 11, 122, 190, 100, 100, 100, 100, 1234, 1};
        unsigned count  = sizeof(values)/sizeof(*values);
        NSMutableArray* array = [[NSMutableArray alloc]initWithCapacity:100];
        for (int ii = 0; ii < count; ii++)
        {
            [array addObject:[NSNumber numberWithInt:values[ii]]];
        }
        NSLog(@"number: %@", [array description]);
       
        NSDictionary* dictionary = countOfValue(array);
        for (NSNumber* numberValue in dictionary)
        {
            NSMutableString* content = [NSMutableString stringWithFormat:@"%@", numberValue];
            NSLog(@"%-10s  count: %@", [content UTF8String], [dictionary objectForKey:numberValue]);
        }
    }
    return 0;
}

